import 'package:color_dart/RgbaColor.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/components/labels/index.dart';
import 'package:im_flutter_sdk_example/jsonserialize/project/data.dart';
import 'package:im_flutter_sdk_example/utils/constants.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:im_flutter_sdk_example/utils/screen.dart';

class Achievement extends StatelessWidget {
  final ProjectItem item;
  Achievement({Key key, this.item}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => {G.pushNamed('/achievement_detail', arguments: item)},
      child: Container(
        color: Colors.white,
        width: double.infinity,
        padding: EdgeInsets.only(top: 16, left: 12.w, right: 12.w),
        child: Column(children: [
          Container(
            height: 154.h,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Image.network(
                  '${G.fullImgPath(item.photos[0])}',
                  width: 214.w,
                  height: 154.h,
                  fit: BoxFit.cover,
                ),
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.only(left: 10),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          '${item.projectName}',
                          style: mainTitleStyle,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                        Row(
                            children: item.words == null
                                ? LabelProject(name: '')
                                : item.words
                                    .map((e) => LabelProject(
                                          name: e,
                                        ))
                                    .toList()),
                        Row(children: [
                          Image.asset(
                            'lib/assets/images/label/label.png',
                            width: 12,
                            height: 12,
                            fit: BoxFit.cover,
                          ),
                          Text(
                            ' ${item.yourWant == null ? '' : item.yourWant.join('、')}',
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                                color: rgba(235, 101, 13, 1), fontSize: 24.nsp),
                          )
                        ])
                      ],
                    ),
                  ),
                )
              ],
            ),
          ),
          Container(
            color: rgba(250, 250, 250, 1),
            margin: EdgeInsets.only(top: 10, bottom: 10),
            padding: EdgeInsets.all(6),
            width: double.infinity,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '${item.yourWantContent}',
                  style: TextStyle(
                      color: rgba(107, 107, 107, 1), fontSize: 28.nsp),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
              ],
            ),
          ),
          Container(
            height: 50,
            decoration: BoxDecoration(
                border: Border(
                    bottom: BorderSide(
                        width: 1, color: Color.fromRGBO(242, 242, 242, 1)))),
            child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      ClipOval(
                        child: Image.network(
                          '${G.fullImgPath(item.accountHead)}',
                          fit: BoxFit.contain,
                          width: 24,
                          height: 24,
                        ),
                      ),
                      Container(
                          child: Text('${item.publishUserName}',
                              style: TextStyle(
                                  color: rgba(163, 163, 163, 1),
                                  fontSize: 24.nsp)),
                          margin: EdgeInsets.only(left: 10)),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Image.asset(
                        'lib/assets/images/label/label_position.png',
                        width: 9,
                        height: 11,
                        fit: BoxFit.contain,
                      ),
                      Container(
                          child: Text('${item.cityName}',
                              style: TextStyle(
                                  color: rgba(163, 163, 163, 1),
                                  fontSize: 22.nsp)),
                          margin: EdgeInsets.only(left: 3)),
                    ],
                  ),
                ]),
          )
        ]),
      ),
    );
  }

  Widget spaceL(Widget child) {
    return Container(
      margin: EdgeInsets.only(left: 10),
      child: child,
    );
  }
}

class AchievementList extends StatefulWidget {
  final isRecommend;
  AchievementList({Key key, this.isRecommend}) : super(key: key);
  @override
  _AchievementListState createState() => _AchievementListState();
}

class _AchievementListState extends State<AchievementList>
    with AutomaticKeepAliveClientMixin {
  List<ProjectItem> projectData = [];
  @override
  void initState() {
    super.initState();
    this.setupData();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return SingleChildScrollView(
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
        child: widget.isRecommend == true && projectData.length > 2
            ? Column(
                children: projectData
                    .getRange(0, 3)
                    .map((e) => Achievement(item: e))
                    .toList())
            : Column(
                children:
                    projectData.map((e) => Achievement(item: e)).toList()),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
  void setupData() async {
    G.showLoading(status: '加载中...');
    var res = await G.req.home.projectList();
    var resData = res.data['data']['list'];
    resData.forEach((e) {
      ProjectItem project = ProjectItem.fromJson(e);
      projectData.add(project);
    });
    this.setState(() {});
    G.dismissLoading();
  }
}
